package com.wdh.kjs.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wdh.kjs.R;
import com.wdh.kjs.http.Httpurl;
import com.wdh.kjs.model.JsonModel_my;
import com.wdh.kjs.util.EditTextLengthUtil;
import com.wdh.kjs.util.ToastUtils;

import java.util.regex.Pattern;

/**
 * Created by CQH on 2016/4/21.
 */
public class UpdateTradePasswordActivity extends BaseTopActivity implements View.OnClickListener {
    private EditText et_utp_oldTradePwd;
    private EditText et_utp_newTradePwd;
    private EditText et_utp_reNewTradePwd;
    private Button bt_utp_confirm;
    private Button bt_utp_reInput;
//    private TextView update_trading_forget;
    private String partyId;
    private JsonModel_my jsonModel_my;
    private LinearLayout update_trading_forget;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update_trading_password);
        SharedPreferences share = getSharedPreferences("logininfo", MODE_PRIVATE);
        partyId = share.getString("loginToken", "");

        initview();
        update_trading_forget.setVisibility(View.VISIBLE);
    }

    private void initview() {
        initTopBar("修改交易密码");
        update_trading_forget = (LinearLayout) findViewById(R.id.update_trading_forget);
        et_utp_oldTradePwd = (EditText) findViewById(R.id.et_utp_oldTradePwd);
        et_utp_newTradePwd = (EditText) findViewById(R.id.et_utp_newTradePwd);
        et_utp_reNewTradePwd = (EditText) findViewById(R.id.et_utp_reNewTradePwd);
        bt_utp_confirm = (Button) findViewById(R.id.bt_utp_confirm);
        bt_utp_reInput = (Button) findViewById(R.id.bt_utp_reInput);

        bt_utp_confirm.setOnClickListener(this);
        bt_utp_reInput.setOnClickListener(this);
        update_trading_forget.setOnClickListener(this);
    }

    private void loadData() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("partyId", partyId);
        params.addBodyParameter("oldTradePwd", et_utp_oldTradePwd.getText().toString());
        params.addBodyParameter("tradePwd", et_utp_newTradePwd.getText().toString());

        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, Httpurl.URL_UPDATE_TRADEPW, params, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                Gson gson = new Gson();
                jsonModel_my = gson.fromJson(responseInfo.result, JsonModel_my.class);

                Log.i("msg", responseInfo.result + "    7777777777");
                Log.i("msg", partyId + jsonModel_my.getReturnMsg());

                if (jsonModel_my.getReturnMsg().equals("success")) {
                    trueDialogShow("密码修改已成功！");
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Intent intent = new Intent();
                            finish();
                            intent.setClass(UpdateTradePasswordActivity.this, SecuritySetting.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                        }
                    }, 2000);
                } else if (jsonModel_my.getReturnMsg().equals("wrongoldpassword")) {
                    falseDialogShow("原密码错误！");
                } else if (jsonModel_my.getReturnMsg().equals("error"))
                    falseDialogShow("修改交易密码失败！");
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                ToastUtils.showShort(getBaseContext(), "网络异常！");
            }
        });

    }

    private void falseDialogShow(String hint) {
        View dialog = (LinearLayout) getLayoutInflater().inflate(R.layout.view_dialog_false, null);
        Button bt_dialog_false_negative = (Button) dialog.findViewById(R.id.bt_dialog_false_negative);
        Button bt_dialog_false_positive = (Button) dialog.findViewById(R.id.bt_dialog_false_positive);
        TextView tv_dialog_false_hint = (TextView) dialog.findViewById(R.id.tv_dialog_false_hint);
        tv_dialog_false_hint.setText(hint);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialog);
        builder.create();
        final AlertDialog dialogShow = builder.show();
        bt_dialog_false_negative.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialogShow.dismiss();
            }
        });
        bt_dialog_false_positive.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialogShow.dismiss();
                et_utp_oldTradePwd.setText("");
                et_utp_newTradePwd.setText("");
                et_utp_reNewTradePwd.setText("");
            }
        });
    }

    private void trueDialogShow(String hint) {
        View dialog = (LinearLayout) getLayoutInflater().inflate(R.layout.view_dialog_true, null);
        TextView tv_dialog_false_hint = (TextView) dialog.findViewById(R.id.tv_dialog_true_hint);
        tv_dialog_false_hint.setText(hint);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialog);
        builder.create();
        final AlertDialog dialogShow = builder.show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_utp_confirm:
                int oldPwdLength = EditTextLengthUtil.check_length(et_utp_oldTradePwd.getText().toString());
                int newPwdLength = EditTextLengthUtil.check_length(et_utp_newTradePwd.getText().toString());
                if (et_utp_oldTradePwd.getText().toString().equals("")) {
                    falseDialogShow("请输入原密码！");
                } else if (oldPwdLength != 0) {
                    if (oldPwdLength > 0) {
                        falseDialogShow("密码位数多于" + EditTextLengthUtil.max_length + "位");
                    } else {
                        falseDialogShow("密码位数少于" + EditTextLengthUtil.min_length + "位");
                    }
                } else if (et_utp_newTradePwd.getText().toString().equals("")) {
                    falseDialogShow("请输入新密码！");
                } else if (newPwdLength != 0) {
                    if (newPwdLength > 0) {
                        falseDialogShow("新密码位数多于" + EditTextLengthUtil.max_length + "位");
                    } else {
                        falseDialogShow("新密码位数少于" + EditTextLengthUtil.min_length + "位");
                    }
                } else if (et_utp_reNewTradePwd.getText().toString().equals("")) {
                    falseDialogShow("请输入确认密码！");
                } else if (!et_utp_newTradePwd.getText().toString().equals(et_utp_reNewTradePwd.getText().toString())) {
                    falseDialogShow("新密码两次输入不一致！");
                } else {
                    loadData();
                }
                break;
            case R.id.bt_utp_reInput:
                et_utp_oldTradePwd.setText("");
                et_utp_newTradePwd.setText("");
                et_utp_reNewTradePwd.setText("");
                break;
            case R.id.update_trading_forget:
                startActivity(new Intent(this, ForgetTradingActivity.class));
                break;
        }
    }

}
